package com.usooft.metaTalent.admin.base;

import com.usooft.framework.core.utils.MapBuilder;
import com.usooft.framework.web.auth.RequiresAuthentication;
import com.usooft.framework.web.upload.FileValidator;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.util.unit.DataSize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/base/file")
@RequiredArgsConstructor
public class FileUploaderController {

    private final FileUploader fileUploader;

    @PostMapping("/upload")
    @RequiresAuthentication
    public ResponseEntity<?> fileUpload(MultipartFile file) {
        String url = null;
        try {
            url = fileUploader.upload(file, FileValidator.of(DataSize.ofMegabytes(10240), "jpg", "png", "jpeg"), "disk:base");
        } catch (IOException e) {
            throw new IllegalArgumentException("上传失败");
        }
        return ResponseEntity.ok(MapBuilder.builder()
                .put("url", url)
                .build());
    }

}
